/*
 * Copyright (c) 2018. czmc All Rights Reserved.
 * Author: czmc
 * Email: zxc6355533@163.com
 * Encoding: UTF-8
 */

package top.stime21.myneighbours.module.settings

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import kotlinx.android.synthetic.main.activity_settings.*
import kotlinx.android.synthetic.main.include_header.*
import me.drakeet.materialdialog.MaterialDialog
import top.stime21.framework.extension.toActivity
import top.stime21.myneighbours.R
import top.stime21.myneighbours.api.LocalDataManager
import top.stime21.myneighbours.utils.CleanMessageUtil


class SettingsActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_settings)
        initView()
    }

    private fun initView() {
        btn_clear.setOnClickListener(l)
        btn_exit.setOnClickListener(l)
        btn_feedback.setOnClickListener(l)
        btn_update.setOnClickListener(l)
        btn_back.setOnClickListener({ finish() })
        tv_title.setText("设置")
    }

    private val l: View.OnClickListener = object : View.OnClickListener {
        override fun onClick(view: View?) {
            when (view) {
                btn_exit -> ensureExit()
                btn_clear -> ensureClear()
                btn_feedback -> toActivity<FeedbackActivity>()
                btn_update ->{}
            }
        }
    }

    fun ensureExit(){
        val dialog = MaterialDialog(this@SettingsActivity).setTitle("")
        dialog.setTitle("温馨提醒")
                .setMessage("是否确认退出此账号，确认退出后将会清空这个账号下的缓存信息")
                .setPositiveButton("确定", View.OnClickListener {
                    LocalDataManager.ins().logout(this@SettingsActivity)
                    dialog.dismiss()
                    finish()
                }).setNegativeButton("取消", {
            dialog.dismiss()
        });
        dialog.show()
    }

    fun ensureClear(){
        val dialog = MaterialDialog(this@SettingsActivity).setTitle("")
        dialog.setTitle("温馨提醒")
                .setMessage("当前存在缓存${CleanMessageUtil.getTotalCacheSize(this)},是否确定清空?")
                .setPositiveButton("确定", View.OnClickListener {
                    CleanMessageUtil.clearAllCache(this)
                    dialog.dismiss()
                }).setNegativeButton("取消", {
            dialog.dismiss()
        })
        dialog.show()
    }

}
